﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class DuyetPhieuTamUng : UserControl
    {
        public DuyetPhieuTamUng()
        {
            InitializeComponent();
        }

        private void DuyetPhieuTamUng_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listPhieuTU = from qt in dc.QuyTrinhMuaHangs
                              where qt.deleted == false &&
                                    qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_TAM_UNG
                              select qt.PhieuTamUng;

            dgvPhieuTamUng.Rows.Clear();
            int count = 1;
            foreach (_DB.PhieuTamUng p in listPhieuTU)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvPhieuTamUng);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = p.MaPhieu;
                r.Cells[2].Value = p.NgayTao;
                r.Cells[3].Value = p.NguoiTao.Ten;
                r.Cells[4].Value = p.ThoiGian;
                r.Cells[5].Value = p.SoTien;

                dgvPhieuTamUng.Rows.Add(r);
            }
        }

        private void btnDuyet_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuTamUng.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuTamUng == maPhieu);
            qt.PhieuTamUng.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            qt.PhieuTamUng.NgayDuyet = DateTime.Now;
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_TAM_UNG;
            dc.SubmitChanges();
            loadData();
        }

        private void btnKhongDuyet_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhieuTamUng.SelectedRows.Count <= 0)
                return;

            // Input ly do
            OtherForms.InputTextForm tf = new OtherForms.InputTextForm("", "Lý do không duyệt");
            tf.ShowDialog();
            if (tf.OK == false)
                return;
            string lydo = tf.getText();

            // go
            string maPhieu = (string)dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuTamUng == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_TAM_UNG;
            qt.PhieuTamUng.MaNguoiDuyet = Program.MaNguoiDungHienTai;
            qt.PhieuTamUng.NgayDuyet = DateTime.Now;
            qt.PhieuTamUng.LyDoKhongDuyet = lydo;
            dc.SubmitChanges();
            loadData();
        }

        private void dgvPhieuTamUng_DoubleClick(object sender, EventArgs e)
        {
            if (dgvPhieuTamUng.SelectedRows.Count <= 0)
                return;

            string maPhieu = (string) dgvPhieuTamUng.SelectedRows[0].Cells[1].Value;
            OtherForms.XemPhieuTamUng f = new OtherForms.XemPhieuTamUng(maPhieu);
            f.Show();
        }
    }
}
